package io.gitee.chearnee.fw.openapi.controller;

import io.gitee.chearnee.fw.openapi.advice.EnableOpenApiResponseAdvice;
import io.gitee.chearnee.fw.openapi.annotation.OpenApiAuth;
import io.gitee.chearnee.fw.openapi.constant.enums.ResultCode;
import io.gitee.chearnee.fw.openapi.exception.OpenApiAsserts;
import io.gitee.chearnee.fw.openapi.model.TokenRequest;
import io.gitee.chearnee.fw.openapi.model.TokenResponse;
import io.gitee.chearnee.fw.openapi.processor.OpenApiProcessor;
import io.gitee.chearnee.fw.web.annotation.EnableResponseAdvice;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @author cn
 * @version 1.0
 * @description
 * @date 2021/9/23 21:04
 */
@RestController
@RequestMapping("/v1")
@AllArgsConstructor
@EnableResponseAdvice(enable = false)
@EnableOpenApiResponseAdvice
@OpenApiAuth(ignoreAccessTokenAuth = true)
public class OpenApiController {

  private final OpenApiProcessor openApiProcessor;

  @PostMapping("getToken")
  public TokenResponse getToken(@RequestBody TokenRequest token,
      @RequestHeader("appId") String appId) {
    if (token == null || token.getGrantType() == null) {
      OpenApiAsserts.error(ResultCode.MISSING_PARAMETER);
    }
    return openApiProcessor.refreshToken(appId);
  }
}
